package com.ljkj.cordial.chat.dao;


import android.text.TextUtils;

import com.lanjiu.pro.business.BusinessProtocolMessageStandard;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * 长连接临时消息
 */
@Entity(nameInDb = "LengthMessageTable")
public class LengthMessageTable {

    public static int SCREEN_ON = 0;
    public static int SCREEN_OFF = 1;
    public static int UNREAD = 0;
    public static int READED = 1;
    public static int All = 2;
    @Id(autoincrement = true)
    private Long id;
    // 服务类型编码+服务编码+模块编码+功能(动作)编码
    @Index(unique = true)
    @Property(nameInDb = "MSG_TYPE")
    private String msgType;

    // 消息内容类型
    @Property(nameInDb = "MSG_CONTENT_TYPE")
    private int msgContentType;

    // 消息发送者id
    @Property(nameInDb = "FROM_ID")
    private String fromId;

    // 消息接收者id
    @Property(nameInDb = "TO_ID")
    private String toId;

    // 消息时间戳
    @Property(nameInDb = "TIMESTAMP")
    private long timestamp;

    // 状态报告   返回类型(成功/失败)
    @Property(nameInDb = "STATUS_REPORT")
    private String statusReport;

    // 消息标识(客户端有用)
    @Property(nameInDb = "UNIQUE_IDENTIFY")
    private String unique_identify;

    // token
    @Property(nameInDb = "TOKEN")
    private String token;

    @Property(nameInDb = "SCREEN_STATE")
    private int screenState;
    @Property(nameInDb = "READ_STATE")
    private int readState;
    @Property(nameInDb = "READ_MSG_NUM")
    private int readMsgNum;

    @Generated(hash = 18267117)
    public LengthMessageTable(Long id, String msgType, int msgContentType, String fromId, String toId, long timestamp, String statusReport, String unique_identify,
                              String token, int screenState, int readState, int readMsgNum) {
        this.id = id;
        this.msgType = msgType;
        this.msgContentType = msgContentType;
        this.fromId = fromId;
        this.toId = toId;
        this.timestamp = timestamp;
        this.statusReport = statusReport;
        this.unique_identify = unique_identify;
        this.token = token;
        this.screenState = screenState;
        this.readState = readState;
        this.readMsgNum = readMsgNum;
    }

    @Generated(hash = 254350827)
    public LengthMessageTable() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMsgType() {
        return this.msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public int getMsgContentType() {
        return this.msgContentType;
    }

    public void setMsgContentType(int msgContentType) {
        this.msgContentType = msgContentType;
    }

    public String getFromId() {
        return this.fromId;
    }

    public void setFromId(String fromId) {
        this.fromId = fromId;
    }

    public String getToId() {
        return this.toId;
    }

    public void setToId(String toId) {
        this.toId = toId;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getStatusReport() {
        return this.statusReport;
    }

    public void setStatusReport(String statusReport) {
        this.statusReport = statusReport;
    }

    public String getUnique_identify() {
        return this.unique_identify;
    }

    public void setUnique_identify(String unique_identify) {
        this.unique_identify = unique_identify;
    }

    public String getToken() {
        return this.token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int getScreenState() {
        return this.screenState;
    }

    public void setScreenState(int screenState) {
        this.screenState = screenState;
    }

    public int getReadState() {
        return this.readState;
    }

    public void setReadState(int readState) {
        this.readState = readState;
    }

    public int getReadMsgNum() {
        return this.readMsgNum;
    }

    public void setReadMsgNum(int readMsgNum) {
        this.readMsgNum = readMsgNum;
    }

    public void insertBean(BusinessProtocolMessageStandard.Head h, boolean isOnScreen) {
        delete(LengthMessageTable.READED);
        setMsgType(h.getMsgType());
        if (!TextUtils.isEmpty(h.getFromId()) && h.getFromId().length() == 9) {
            setFromId(h.getFromId().substring(0, h.getFromId().length() - 1));
        } else {
            setFromId(h.getFromId());
        }
        setToId(h.getToId());
        setMsgContentType(h.getMsgContentType());
        setStatusReport(h.getStatusReport());
        setTimestamp(h.getTimestamp());
        setUnique_identify(h.getUniqueIdentify());
        setToken(h.getToken());
        setScreenState(isOnScreen ? LengthMessageTable.SCREEN_ON : LengthMessageTable.SCREEN_OFF);
        setReadState(LengthMessageTable.UNREAD);
        System.out.println("insert msgType->" + h.getMsgType());
        try {
            LengthMessageTable us = getTableByExistMsgType(h.getMsgType());
            if (us != null) {
                setId(us.getId());
                setReadMsgNum(us.getReadMsgNum() + 1);
            }
            DbController.getInstance().getWrite().getLengthMessageTableDao().insertOrReplace(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void test() {
        //                LengthMessageTable lengthMessageTable = new LengthMessageTable();
//                lengthMessageTable.insertBean(head, OnScreenUtil.isOpen(ChatApp.getInstance()));
//                if (activity == null && activity.isFinishing()) {
//                    return;
//                }
//                activity.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (onChatNoDataListener != null) {
//                            onChatNoDataListener.onHead(LengthMessageTable.getOfflineList(LengthMessageTable.UNREAD));
//                        }
//                    }
//                });
    }

    public static LengthMessageTable getTableByExistMsgType(String msgType) {
        LengthMessageTable b = DbController.getInstance().getRead().getLengthMessageTableDao().queryBuilder().where(LengthMessageTableDao.Properties.MsgType.eq(msgType)).unique();
        return b;
    }

    public static List<LengthMessageTable> getOfflineList(int readState) {
        List<LengthMessageTable> rlist = new ArrayList<>();
        List<LengthMessageTable> list = DbController.getInstance().getRead().getLengthMessageTableDao().queryBuilder().list();
        if (list != null) {
            for (LengthMessageTable lengthMessageTable : list) {
                if (readState == lengthMessageTable.getReadState() || readState == LengthMessageTable.All) {
                    rlist.add(lengthMessageTable);
                }
            }
        }
        return rlist;
    }

    public static void delete(int readState) {
        Query<LengthMessageTable> query = DbController.getInstance().getRead().getLengthMessageTableDao().queryBuilder().build();
        List<LengthMessageTable> list = query.list();
        if (list != null && !list.isEmpty()) {
            for (LengthMessageTable table : list) {
                if (readState == table.getReadState() || readState == LengthMessageTable.All) {
                    System.out.println("delete->" + table.getMsgType());
                    DbController.getInstance().getWrite().getLengthMessageTableDao().delete(table);
                }
            }
        }
    }

}
